var app = getApp();
var c = require('../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'arrayr',
      success: function(res) {
        var searcharrays = res.data;
        that.setData({
          searcharrays: searcharrays
        });
      },
      fail: function(res) {},
      complete: function(res) {},
    })


    c.ajax(
      app.url + 'hotsearch', {},
      res => {
        this.setData({
          array: res.data
        })
      }
    )
    c.ajax(
      app.url + 'search', {
        content: '测试'
      },
      res => {

        console.log(res);
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  searchinfo: function(e) {
    var value = this.data.valueinfo;
    var searcharrays = this.data.searcharrays;
    var show = 1; //如果缓存没有此搜索内容为1
    if (searcharrays == undefined || searcharrays == '') {
      searcharrays = [];
    } else {
      for (var i = 0; i < searcharrays.length; i++) {
        if (searcharrays[i] == value) {
          show = 0; //如果找到缓存此搜索内容为0
        }
      }

    }
    if (show == 1) {  //如果没有缓存则需要把 搜索内容 添加到缓存
      searcharrays.push(value);
      wx.setStorage({
        key: 'arrayr',
        data: searcharrays,
      })

      this.setData({
        searcharrays: searcharrays
      })
    }


    wx.navigateTo({
      url: '/pages/searchlist/searchlist?content=' + value,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })

  },
  searchchanle: function(e) {
    var valueinfo = e.detail.value;
    this.setData({
      valueinfo: valueinfo
    })
  },
  clearhist: function() {
    wx.setStorage({
      key: 'arrayr',
      data: '',
    })
    this.setData({
      searcharrays: ''
    })
  },
  nameinfo: function(e) {
    var content = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/searchlist/searchlist?content=' + content,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })

  }
})